﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;
using System.Configuration;
using System.Text;

namespace kTudu.Web.Helpers
{
    public static class Logger
    {
        #region Properties

        private static EventLog _log = null;
        private static string _sourceName = null;
        private static string _logName = null;

        #endregion

        #region Constructors

        static Logger()
        {
            _sourceName = ConfigurationManager.AppSettings["SourceName"];
            _logName = ConfigurationManager.AppSettings["EventLogName"];

            if (!EventLog.SourceExists(_sourceName))
                EventLog.CreateEventSource(_sourceName, _logName);

            _log = new EventLog
            {
                Source = _sourceName,
                Log = _logName
            };
        }

        #endregion

        #region Public Methods

        public static void Write(string message)
        {
            try
            {
                _log.WriteEntry(message, EventLogEntryType.Error);
            }
            catch (Exception logEx)
            {
                try
                {
                    _log.WriteEntry(logEx.Message, EventLogEntryType.Error);
                }
                catch
                {
                }
            }
        }

        public static void Write(Exception ex)
        {
            try
            {
                _log.WriteEntry(GetMessage(ex), EventLogEntryType.Error);
            }
            catch (Exception logEx)
            {
                try
                {
                    _log.WriteEntry(logEx.Message, EventLogEntryType.Error);
                }
                catch
                {
                }
            }
        }

        #endregion

        #region Private Methods

        private static string GetMessage(Exception ex)
        {
            var sb = new StringBuilder();

            sb.AppendLine();
            sb.Append("---- Excepción ----");
            sb.AppendLine();
            sb.Append(string.Format("Mensaje: {0}", ex.Message));
            sb.AppendLine();
            sb.Append(string.Format("Objeto: {0}", ex.TargetSite.ReflectedType.Name));
            sb.AppendLine();
            sb.Append(string.Format("Método: {0}", ex.TargetSite.Name));
            sb.AppendLine();
            sb.AppendLine();
            sb.Append("StackTrace:");
            sb.Append(ex.StackTrace);

            sb.AppendLine();
            sb.AppendLine();
            sb.Append("---- Last Inner Excepción ----");
            sb.AppendLine();
            sb.Append(string.Format("Mensaje: {0}", ex.GetBaseException().Message));
            sb.AppendLine();
            sb.Append(string.Format("Objeto: {0}", ex.GetBaseException().TargetSite.ReflectedType.Name));
            sb.AppendLine();
            sb.Append(string.Format("Método: {0}", ex.GetBaseException().TargetSite.Name));
            sb.AppendLine();
            sb.AppendLine();
            sb.Append("StackTrace:");
            sb.Append(ex.GetBaseException().StackTrace);
            sb.AppendLine();
            sb.AppendLine();

            if (ex.InnerException != null)
            {
                var inner = ex.InnerException.InnerException;

                if (inner != null)
                    sb.Append("---- Inner Exceptions ----");

                while (inner != null)
                {
                    sb.AppendLine();
                    sb.Append(string.Format("Mensaje: {0}", inner.Message));

                    inner = inner.InnerException;
                }
            }

            return sb.ToString();
        }

        #endregion
    }
}